<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Type;
use App\Article;

class WelcomeController extends Controller
{
    public function zcjd()
    {
        $picture = 'zcjd';
        $articles = Article::type(3)->get();
//        $types = Type::all();
//        foreach ($types as $type) {
//            $type->text = str_replace('http://www.bjepb.gov.cn/eportal/ui?pageId=306808&currentPage=1&moduleId=b98575028a76478eb0f1f8cc1b48f7f4',
//                'http://114.251.10.92:8080/XYPT/?nsukey=KGJlJrai0MbCHlA3LTxLGPqGhA9hQeQYG4S7xTB8ckqEMV2v1EoEGd8qF5MmUUjDsTTGIEcg6ZKQyepAcrICU%2Fgv3uWXuZAkBnNBTAdDX1B%2BqPZmI8os4c5stO7PMb4nUFQ9tmb26YiBqy%2FeRRoBQ6nY798SFWErckEISartu4y34oQHPXVWLfNTgJ%2FsIEnm', $type->text);
//            $type->text  = str_replace('http://fssb.easy4work.xyz/bazn', 'http://fs-sb.yp-erp.com/bazn',$type->text );
//            $type->update();
//        }
        return view('articles', compact('picture', 'articles'));
    }

    public function sjd()
    {
        $picture = 'sjd';
        $articles = Article::type(4)->get();
        return view('articles', compact('picture', 'articles'));
    }

    public function flzs()
    {
        $picture = 'flzs';
        $articles = Article::type(0)->get();
        return view('articles', compact('picture', 'articles'));
    }

    public function bgxz()
    {
        $picture = 'bgxz';
        $articles = Article::type(1)->get();
        return view('articles', compact('picture', 'articles'));
    }

    public function bazn()
    {
        $picture = 'bazn';
        $articles = Article::type(2)->get();
        return view('articles', compact('picture', 'articles'));
    }

    public function tip()
    {
        return view('tip');
    }

    public function search()
    {
        $types = Type::where('parent_id', 0)->get();
        foreach ($types as $key => $type) {
            $children = Type::where('parent_id', $type->id)->get();
            $types[$key]['children'] = $children;
        }
        return view('search', compact('types'));
    }

    public function doSearch()
    {
        $data = request()->get('searchipt');
        $types = Type::where('title', 'like', '%' . $data . '%')->where('parent_id', '<>', 0)->get();
        $first = Type::where('title', 'like', '%' . $data . '%')->where('parent_id', 0)->get();
        foreach ($first as $item) {
            $second = Type::where('parent_id', $item->id)->get();
            foreach ($second as $it) {
                $types->push($it);
            }
        }
        $types = $types->unique();
        return view('searchResults', compact('types'));
    }

    public function type($id)
    {
        $type = Type::find($id);
        return view('type', compact('type'));
    }

    public function images()
    {
        $disk = \Storage::disk('qiniu');
        $images = $disk->files('other');
        return view('images', compact('images'));
    }

    public function uploadImages()
    {
        $disk = \Storage::disk('qiniu');
        $file = request()->file('file');
        $name = request()->get('name');
        $disk->put($name, file_get_contents($file->getRealPath()));
        $urls = [];
        $cdn_url = env('QINIU_DOMAINS_DEFAULT').'/'.$name;
        array_push($urls,$cdn_url);
        $this->refreshUrl($urls);
        return redirect()->back();
    }

    public function refreshUrl(array $urls)
    {
        $accessKey = env('QINIU_ACCESS_KEY');
        $secretKey = env('QINIU_SECRET_KEY');
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        $cdnManager = new \Qiniu\Cdn\CdnManager($auth);
        list($refreshResult, $refreshErr) = $cdnManager->refreshUrls($urls);
        if ($refreshErr != null) {
            return '刷新失败';
        } else {
            return '刷新成功';
        }
    }
}
